package com.gitee.openviducn.inspector.auth;

import com.google.common.collect.Maps;

import java.util.Map;

/**
 * @Author: https://gitee.com/wesleyOne
 * @Date: 04.18 2020
 * 参考[https://github.com/alibaba/Sentinel/]控制台项目
 */
public interface AuthService<R> {

    String USER_ID ="uid";
    String USERNAME ="uname";
    String ROLE ="role";

    /**
     * Get the authentication user.
     *
     * @param request the request contains the user information
     * @return the auth user represent the current user, when the user is illegal, a null value will return.
     */
    AuthUser getAuthUser(R request);

    /**
     * 角色类型
     */
    enum SystemRole {

        /**
         * 录制
         */
        RECORD,
        /**
         * webhook_cdr
         */
        WEBHOOK_CDR,
        /**
         * 管理员
         */
        ADMIN
    }

    /**
     * Represents the current user.
     */
    interface AuthUser {

        /**
         * 鉴权
         */
        boolean authTarget(String target, SystemRole[] systemRoles);

        Long getId();

        String getUsername();

        SystemRole getRole();

        default Map<String, String> toMap() {
            Map<String, String> userMap = Maps.newHashMap();
            userMap.put(USER_ID, getId().toString());
            userMap.put(USERNAME, getUsername());
            userMap.put(ROLE, getRole().name());
            return userMap;
        }

    }
}
